#include "core.h"

#include "../../ExEngine/common/def.h"
#include "../../ExEngine/engine/object.h"
#include "../../ExEngine/engine/engine.h"

#include "../../ExEngine/input/dinput.h"
#include "../../ExEngine/render/d3d.h"
#include "../../ExEngine/sound/dsound.h"

using namespace Game;

const bool game_DebugLevel = true;

PlayMode Game::g_GamePlayMode = SIDE_SCROLL;
CLevel* Game::g_CurrentLevel = NULL;
CPlayer* Game::g_Player = NULL;
Vector3f Game::g_Checkpoint;
Vector3f Game::g_TransitionCameraAnchor;
Vector3f Game::g_TransitionCameraTarget;

HRESULT Game::Shutdown()
{
	if(g_Player)
	{
		g_Player->Release();
		g_Player = NULL;
	}

	if(g_CurrentLevel)
	{
		g_CurrentLevel->Release();
		g_CurrentLevel = NULL;
	}

	return S_OK; 
}


HRESULT Game::Update(const float dt)
{
	g_CurrentLevel->Update(dt);
	if(g_Player)g_Player->Update(dt);

	g_CurrentLevel->Draw();
	if(g_Player)g_Player->Draw();

	if(CEngine::GetInstance()->GetInput()->isKeyReleased(DIK_B))
		Game::g_GamePlayMode = (Game::g_GamePlayMode == Game::CHASE) ? Game::SIDE_SCROLL : Game::CHASE;
	
	if(CEngine::GetInstance()->GetInput()->isKeyReleased(DIK_R))
		CEngine::GetInstance()->GetRenderer()->ReloadResources(CD3D9Device::RESOURCE_ALL);

	if(CEngine::GetInstance()->GetInput()->isKeyReleased(DIK_Z))
		g_Checkpoint = g_Player->getPosition();
	if(CEngine::GetInstance()->GetInput()->isKeyReleased(DIK_F))
		g_Player->setPosition(g_Checkpoint);

	if(CEngine::GetInstance()->GetInput()->isKeyReleased(DIK_X))
		CEngine::GetInstance()->GetRenderer()->SetRenderMode( (CEngine::GetInstance()->GetRenderer()->GetRenderMode() == CD3D9Device::SURFACE_NORMAL) ? CD3D9Device::DEFAULT : CD3D9Device::SURFACE_NORMAL);

	if(CEngine::GetInstance()->GetInput()->isKeyReleased(DIK_G))
		Game::g_GamePlayMode = Game::g_GamePlayMode == Game::FREE_FLY ? Game::SIDE_SCROLL : Game::FREE_FLY;


	CEngine::GetInstance()->GetRenderer()->DrawLine(Vector3f(), Vector3f(1,0,0));
	CEngine::GetInstance()->GetRenderer()->DrawPoint(Vector3f(0,1,0), 0.1f);
	return S_OK; 
}

HRESULT Game::Initalize()
{	
	/*EESound::IAudio* pTest = NULL;
	CEngine::GetInstance()->GetSound()->createSound("test.mp3", &pTest);
	pTest->Control(PLAY_ONCE);*/

	CCamera::cameraData cameraSettings = {2.0f,100.0f,45.0f}; 
	CEngine::GetInstance()->GetCamera()->initalizeCamera(cameraSettings); 
	CEngine::GetInstance()->GetCamera()->initalizeProjection(1,35,DEGTORAD(90.0f));

	CEngine::GetInstance()->GetCamera()->setState(CCamera::STATIC); 
	CEngine::GetInstance()->GetCamera()->setUpdateFunc(Game::CameraMovement);
	
	CEngine::GetInstance()->GetCamera()->setPosition(Vector3f(0,2.5,-5));
	CEngine::GetInstance()->GetCamera()->setDirection(Vector3f(0,-0.1f,1));

	g_Player = EE_NEW CPlayer();	
	if(FAILED(CEngine::GetInstance()->CreateObject(g_Player)))
		throw EE_EXCEPTION("Failed to create player.");
	g_Player->setPosition(Vector3f(0,1,0));

	g_CurrentLevel = EE_NEW CLevel();
	if(FAILED(CEngine::GetInstance()->CreateObject(g_CurrentLevel)))
		throw EE_EXCEPTION("Failed to create level.");


	return S_OK; 
}